/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 12/19/2010 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Utility;
using BusinessObject;
using System.Xml;

namespace DataAccessLayer
{
    /// <summary>
    /// Define MatHangDao class. This class inherit from  MatHangDaoBase class.
    /// If you want add new function, you must define here.
    /// </summary>
    public class MatHangDao : MatHangDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public MatHangDao() { }
        static MatHangDao()
        {
            // Init logger.
            logger = LoggingUtility.GetLogger(typeof(MatHangDao).FullName);
        }
        public static int GenerateKey()
        {
            int id = 0;
            String methodName = "GenerateKey";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spGenerateProductKey");

          
            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                id = (int)ManagerDao.GetParameterValue(cmd, "returnValue");               
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
              
            }
            return id;
        }
        public static List<MatHangResult> GetMatHangByLoaiMatHang(int maLoaiMatHang)
        {
            String methodName = "GetMatHangByLoaiMatHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<MatHangResult> listMatHang = new List<MatHangResult>();
                DbCommand cmd = ManagerDao.GetStoredProcCommand("spGetMatHangByLoaiMatHangToControl");
                //Add value MaLoaiMatHang  paramater.
                ManagerDao.AddInParameter(cmd, "MaLoaiMatHang", DbType.Int32, maLoaiMatHang);
                using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        listMatHang.Add(GetMatHangToControlResultByDataReader(reader));                       
                    }
                    reader.Close();
                }
                return listMatHang;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
       
        private static MatHangResult GetMatHangToControlResultByDataReader(IDataReader reader)
        {
            String methodName = "GetMatHangToControlResultByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                MatHangResult result = new MatHangResult();
                //read MaMatHang field data.
                result.MaMatHang = ManagerDao.GetstringDataFromDataReader(reader, "MaMatHang");
                
                //read TenMatHang field data.
                result.TenMatHang = ManagerDao.GetstringDataFromDataReader(reader, "TenMatHang");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        public static MatHang GetMatHangByIDInPhieuNhaphang(string maMatHang)
        {
            String methodName = "GetMatHangByIDInPhieuNhaphang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                MatHang result = null;
                DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetMatHangByIDInPhieuNhaphang");
                //Add value "MaMatHang"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "MaMatHang", DbType.AnsiString, maMatHang);
                IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
                if (reader.Read())
                {
                    result = GetMatHangByIDInPhieuNhaphangByDataReader(reader);

                }
                reader.Close();
                return result;
            }
            catch (System.Exception e)
            {
                //write error message to log file.
                logger.Error("Get", e);
                return null;
            }
        }
        public static MatHang GetMatHangByMaPhuTungInPhieuNhaphang(string maPT)
        {
            String methodName = "GetMatHangByMaPhuTungInPhieuNhaphang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                MatHang result = null;
                DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetMatHangByMaPhuTungInPhieuNhaphang");
                //Add value "MaMatHang"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "MaPhuTung", DbType.AnsiString, maPT);
                IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
                if (reader.Read())
                {
                    result = GetMatHangByIDInPhieuNhaphangByDataReader(reader);

                }
                reader.Close();
                return result;
            }
            catch (System.Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        private static MatHang GetMatHangByIDInPhieuNhaphangByDataReader(IDataReader reader)
        {
            String methodName = "GetMatHangByIDInPhieuNhaphangByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                MatHang result = new MatHang();
                //read MaMatHang field data.

                result.TenNuocSX = ManagerDao.GetstringDataFromDataReader(reader, "SubCategoryName");
                result.Hieu = ManagerDao.GetstringDataFromDataReader(reader, "Hieu");
                result.MaLoaiHangHoa = ManagerDao.GetintDataFromDataReader(reader, "MaLoaiHangHoa");
                result.DVT = ManagerDao.GetintDataFromDataReader(reader, "DVT");
                result.MaPhuTung = ManagerDao.GetstringDataFromDataReader(reader, "MaPhuTung");
                result.TenMatHang = ManagerDao.GetstringDataFromDataReader(reader, "TenMatHang");
                result.MaMatHang = ManagerDao.GetstringDataFromDataReader(reader, "MaMatHang");
                result.MaXe = ManagerDao.GetintDataFromDataReader(reader, "MaXe");
              

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        public static bool Publish(MatHang obj)
        {
            bool result = false;
            String methodName = "PublishMatHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spPublishMatHang");

            //Add value "MaMatHang"  paramater.
            ManagerDao.AddInParameter(cmd, "MaMatHang", DbType.AnsiString, obj.MaMatHang);
            
            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                int id = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
        public static int GetSoLuongTonByDonViTinh(string maMatHang,int maKho,int maDonViTinh)
        {
            int result = 0;
            String methodName = "GetSoLuongTonByDonViTinh";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
              
                DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetSoLuongTonMatHang");
                //Add value "MaMatHang"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "MaMatHang", DbType.AnsiString, maMatHang);
                ManagerDao.AddInParameter(dbCommnand, "MaDonViTinh", DbType.Int32, maDonViTinh);
                ManagerDao.AddInParameter(dbCommnand, "MaKho", DbType.Int32,ConvertValueObject.CastObjValueToInsertDB(maKho));
                IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
                if (reader.Read())
                {
                    result =int.Parse(reader.GetValue(0).ToString());

                }
                reader.Close();
                return result;
            }
            catch (System.Exception e)
            {
                //write error message to log file.
                logger.Error("GetSoLuongTonByDonViTinh", e);
              
            }
            return result;
        }
        public static List<MatHangResult> GetMatHangByChungXe(int chungXe)
        {
            String methodName = "GetMatHangByChungXe";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<MatHangResult> listMatHang = new List<MatHangResult>();
                DbCommand cmd = ManagerDao.GetStoredProcCommand("spGetMatHangByChungXe");
                //Add value PageIndex  paramater.
                ManagerDao.AddInParameter(cmd, "ChungXe", DbType.Int32, chungXe);          
                using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        listMatHang.Add(GetMatHangResultByDataReader(reader));
                     
                    }

                    reader.Close();
                }

                return listMatHang;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
    }
}
        #endregion